package com.bug;

import com.bug.mamage.WordInfoManage;
import com.model.ExplainDetail;
import us.codecraft.webmagic.selector.Html;

import java.util.List;

public abstract class CatchStrategy {


    protected WordInfoManage wordInfoManage;

    public void setWordInfoManage(WordInfoManage wordInfoManage) {
        this.wordInfoManage = wordInfoManage;
    }

    protected abstract class Method {
        private String url;
        protected CatchStrategy strategy;
        public abstract void accept(Html html, String word);

    }
    protected class Explain extends Method {
        public void accept(Html html, String word) {
            String text = onCatchPage(this, html, word);
            wordInfoManage.getWordInfo(word).setExplained(text);
            fullCallManage(word);
        }
    }
    protected class Understand extends Method {
        public void accept(Html html, String word) {
            String text = onCatchPage(this, html, word);
            wordInfoManage.getWordInfo(word).setUnderstand(text);
            fullCallManage(word);
        }
    }
    protected class Definitions_json extends Method {
        public void accept(Html html, String word) {
            List<ExplainDetail> details = onCatchPage(this, html, word);
            wordInfoManage.getWordInfo(word).setDetails(details);
            fullCallManage(word);
        }
    }
    protected class Appear extends Method {
        public void accept(Html html, String word) {
            String accept = onCatchPage(this, html, word);
            wordInfoManage.getWordInfo(word).setAppear(accept);
            fullCallManage(word);
        }
    }
    protected class Synonyms extends Method {
        public void accept(Html html, String word) {
            onCatchPage(this, html, word);
            fullCallManage(word);
        }
    }
    protected class Type extends Method {
        public void accept(Html html, String word) {
            onCatchPage(this, html, word);
            fullCallManage(word);
        }
    }


    protected class Example extends Method {
        public void accept(Html html, String word) {
            onCatchPage(this, html, word);
        }
    }

    protected Explain EXPLAIN = new Explain();
    protected Understand UNDERSTAND = new Understand();
    protected Definitions_json DEFINITIONS_JSON = new Definitions_json();
    protected Appear APPEAR = new Appear();
    protected Synonyms SYNONYMS = new Synonyms();
    protected Type TYPE = new Type();

    protected Example EXAMPLE = new Example();



    // 为了保证以后拓展行, 考虑到一个wordInfo可能是通过捕捉多个URL拼凑的, 或一个word例句, 可能是会爬取多个URL
    public class CatchPage{
        private String url;
        private Html html;
        private String word;
        private List<Method> chain;

        public void setUrl(String url) {
            this.url = url;
        }

        public String getUrl() {
            return url;
        }

        public void setChain(List<Method> chain) {
            this.chain = chain;
        }

        public void setHtml(Html html) {
            this.html = html;
        }

        public Html getHtml() {
            return html;
        }

        public String getWord() {
            return word;
        }

        public void setWord(String word) {
            this.word = word;
        }
    }

    /**
     * 根据word给出爬虫URL与METHOD的关系
     * @return
     */
    public abstract List<CatchPage> getInfoCatchList(String word);

    /**
     * 根据word给出爬取例与METHOD的关系
     * @return
     */
    public abstract List<CatchPage> getExampleCatch(String word);

    public abstract String onCatchPage(Explain method, Html html, String word);
    public abstract String onCatchPage(Understand method, Html html, String word);
    public abstract List<ExplainDetail> onCatchPage(Definitions_json method, Html html, String word);
    public abstract String onCatchPage(Appear method, Html html, String word);
    public abstract void onCatchPage(Synonyms method, Html html, String word);
    public abstract void onCatchPage(Type method, Html html, String word);
    public abstract void onCatchPage(Example method, Html html, String word);

    public void catchPage(CatchPage catchPage){
        catchPage.chain.forEach(chain -> {
            chain.url = catchPage.url;
            chain.accept(catchPage.getHtml(), catchPage.getWord());
        });
    }

    public void fullCallManage(String word) {
        if(wordInfoManage.isFull(word)) {
            wordInfoManage.fullCallBack(word);
        }
    }
}
